#!/usr/bin/env python
    ##########################################################################
    ##(c) 2008 Bastian Damman                                               ##
    ##                                                                      ##
    ##This file is part of Kaboom-O-rama.                                   ##
    ##                                                                      ##
    ##Kaboom-O-rama is free software: you can redistribute it and/or modify ##
    ##it under the terms of the GNU General Public License as published by  ##
    ##the Free Software Foundation, either version 3 of the License, or     ##
    ##(at your option) any later version.                                   ##
    ##                                                                      ##
    ##Kaboom-O-rama is distributed in the hope that it will be useful,      ##
    ##but WITHOUT ANY WARRANTY; without even the implied warranty of        ##
    ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         ##
    ##GNU General Public License for more details.                          ##
    ##                                                                      ##
    ##You should have received a copy of the GNU General Public License     ##
    ##along with Kaboom-O-rama.  If not, see <http://www.gnu.org/licenses/>.##
    ##########################################################################
from pygame import draw, mouse, MOUSEBUTTONDOWN, MOUSEBUTTONUP

class Camera:
    offset = (0, 0)
    def __init__(self, screen):
        self.screen = screen
        self.width = self.screen.get_width()
        self.height = self.screen.get_height()
        
        self.hold = False #mouse move
        self.hold_pos = True
    
    def move(self, addx, addy):
        self.offset = (addx + self.offset[0],
                       addy + self.offset[1])
    
    def get_pos(self):
        return(self.get_x(), self.get_y())
    
    def get_x(self):
        return self.offset[0]
    
    def get_y(self):
        return self.offset[1]
    
    def mouse_input(self, events):
        mb1, mb2, mb3 = None, None, None
        for e in events:
            if e.type == MOUSEBUTTONDOWN:
                if e.button == 1:
                    mb1 = True
                elif e.button == 2:
                    mb2 = True
                elif e.button == 3:
                    mb3 = True
            if e.type == MOUSEBUTTONUP:
                if e.button == 1:
                    mb1 = False
                elif e.button == 2:
                    mb2 = False
                elif e.button == 3:
                    mb3 = False
                    
        if (mb1 == True and mb3 == True) or mb2 == True:
            self.hold = True
            mouse_pos = mouse.get_pos()
            self.hold_pos = (self.get_x() + mouse_pos[0],
                             self.get_y() + mouse_pos[1])
            print "hold = true"
        elif mb1 == False or mb3 == False or mb2 == False:
            self.hold = False
            print "hold = false"
            
        if self.hold == True:
            mouse_pos = mouse.get_pos()
            real_mouse_pos = (self.get_x() + mouse_pos[0],
                             self.get_y() + mouse_pos[1])
            temp = (self.hold_pos[0] - real_mouse_pos[0],
                    self.hold_pos[1] - real_mouse_pos[1])
            self.move(temp[0], temp[1])
            
        
    
    def blit_line(self, x1, y1, x2, y2, color = (255,255,255,255), width = 1):
        sx = self.get_x()
        sy = self.get_y()
        pos1 = (x1 - sx, y1 - sy)
        pos2 = (x2 - sx, y2 - sy)
        if pos1[0] < 0 and pos2[0] < 0: return
        if pos1[0] > self.width and pos2[0] > self.width: return
        if pos1[1] < 0 and pos2[1] < 0: return
        if pos1[1] > self.height and pos2[1] > self.height: return
        draw.line(self.screen, color, pos1, pos2, width)
    
    def static_blit_line(sel, x1, y1, x2, y2, color = (255,255,255,255), width = 1):
        pos1 = (x1, y1)
        pos2 = (x2, y2)
        if pos1[0] < 0 and pos2[0] < 0: return
        if pos1[0] > self.width and pos2[0] > self.width: return
        if pos1[1] < 0 and pos2[1] < 0: return
        if pos1[1] > self.height and pos2[1] > self.height: return
        draw.line(self.screen, color, pos1, pos2, width)
    
    def static_blit(self, img, pos, blend = 0):
        self.screen.blit(img, pos, None, blend)
    
    def blit(self, img, pos, blend = 0):
        x = pos[0]
        y = pos[1]
        xc = self.get_x()
        yc = self.get_y()
        x -= xc
        y -= yc
        if x + img.get_width() >= 0 and y + img.get_height() >= 0:
            if x < self.width and y < self.height:
                self.screen.blit(img, (x, y), None, blend)